
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.*;

/**
 * Class contoining the informaion needed to represent an application
 */
class app {
    String name;    // Application name
    int bw;         // required bandwidth

    public app() {

    }

    public app(String appName, int BW) {
        name = appName;
        bw = BW;
    }
}

/**
 * File servers receive a request from a download server. It then
 * looks in it's database (Vector or array) if application is avail-
 * able. If application is not available, zero bandwidth is returned
 */
public class FS implements DSFS {
    private Vector<app> apps = new Vector<app>();
    private Random r = new Random(); // Random generator
    private int av_bw;

    public FS() {
        // Application database is made
        apps.add(new app("SW1", 10));
        apps.add(new app("SW2", 15));
        apps.add(new app("MSWORD", 35));
        apps.add(new app("NetBeans", 30));
    }

    public int getAppBW(String name) throws RemoteException {
        av_bw = r.nextInt(50);
        System.err.println("Fileserver has bw " + av_bw);

        for (int i = 0; i < apps.size(); i++) {
            if(apps.elementAt(i).name.equals(name) && apps.
                    elementAt(i).bw <= av_bw) {
                return av_bw;
            }
        }
        return 0;
    }

    public String getAppName(String name) throws RemoteException {
        return null;
    }

    

    public static void main(String args[]) {
        String arg = (args.length < 1) ? null : args[0];

        try {
            FS fileserver = new FS();
            DSFS stub = (DSFS) UnicastRemoteObject.exportObject
                    (fileserver, 0);

            Registry reg = LocateRegistry.getRegistry();
            reg.rebind(arg, stub);

            System.err.println("File server ready");
            
        } catch (Exception e) {
            System.err.println("File server exception: "
                    + e.toString());
        }
    }
    
}
